package com.yyw.gulimall.coupon.controller.feign;

import com.yyw.common.utils.R;
import com.yyw.gulimall.coupon.controller.feign.dto.MemFullLaddersDto;
import com.yyw.gulimall.coupon.service.MemberPriceService;
import com.yyw.gulimall.coupon.service.SkuFullReductionService;
import com.yyw.gulimall.coupon.service.SkuLadderService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author 易煜玮
 * @ClassName SkuInfoController.java
 * @Description Sku详情
 * @createTime 2020年11月15日 13:38:00
 */
@RestController
@RequestMapping("feign/skuInfo")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SkuInfoController {

    /**
     * 满减优惠接口
     */
    private final SkuFullReductionService fullReductionService;

    /**
     * 满几件优惠接口
     */
    private final SkuLadderService ladderService;

    /**
     * 会员价格接口
     */
    private final MemberPriceService memberPriceService;


    /**
     * 远程调用 保存和SKU相关的信息
     * sms->sms_sku_ladder\sms_sku_full_reduction\sms_member_price
     * @return
     */
    @PostMapping("/saveSkuRelevant")
    @Transactional
    public R saveSkuRelevant(@RequestBody MemFullLaddersDto dto) {
        if (!Objects.equals(null, dto.getSkuFullReductionDto())) {
            fullReductionService.save(dto.getSkuFullReductionDto());
        }
        if(!Objects.equals(null, dto.getSkuLadderDto())) {
            ladderService.save(dto.getSkuLadderDto());
        }
        if (!CollectionUtils.isEmpty(dto.getMemberPriceDtos())) {
            memberPriceService.saveBatch(dto.getMemberPriceDtos());
        }
        return R.ok();
    }
}
